<?php

class UserIdentity extends CUserIdentity
{
	private $_id   = null;
	private $_code = null;

	// User access data (login => password).	
	private $_usersLoginData = array(
		'root' => 'qwerty'
	);
	
	const ERROR_CAPTCHA_INVALID = 101;
	
	public function __construct($login, $password, $code)
	{
		$this->_code = $code;
		
		parent::__construct($login, $password);
	}
	
	static public function showCaptchaSecurityImages()
	{
		$width      = 240;
		$height     = 80;
		$characters = 6;
		$font       = 'cour';
		
		$params = array(
			'width'  => $width,
			'height' => $height,
			'characters' => $characters,
			'font' => $font
		);
		
		new CaptchaSecurityImages($params);
	}
	
    public function authenticate()
    {
        $record = null;
        
        if (array_key_exists($this->username, $this->_usersLoginData)) {
        	$record = array(
        		'login'    => $this->username,
        		'password' => $this->_usersLoginData[$this->username]
        	);
        }
        
        if (!(array_key_exists('security_code', $_SESSION) && $_SESSION['security_code'] == $this->_code)) {
        	$this->errorCode = self::ERROR_CAPTCHA_INVALID;
        } else if ($record === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($record['password'] !== $this->password) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = 1;
        	$this->errorCode = self::ERROR_NONE;
        }
        
        return !$this->errorCode;
    }
    
    public function getId()
    {
    	return $this->_id;
    }
    
    static function checkAuth()
    {
    	if (!Yii::app()->user->getIsGuest()) {
    		return true;
    	} else {
    		return false;
    	}
    }
}